package com.example.mongodb.demo.dao;

import com.example.mongodb.demo.model.entity.Shop;
import com.example.mongodb.demo.utils.DataUtils;
import com.mongodb.client.result.UpdateResult;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.regex.Pattern;

@Repository
public class ShopDao {

    @Autowired
    private MongoTemplate mongoTemplate;

    public Shop findById(String id) {
        return mongoTemplate.findById(id,Shop.class);
    }

    public Shop save(Shop blog) {
        return mongoTemplate.save(blog,"shop");
    }

    public void delete(Shop blog) {
        Query query=new Query(Criteria.where("id").is(blog.getId()));
        mongoTemplate.remove(query,Shop.class);
    }

    public long update(Shop blog) {
        Query query=new Query(Criteria.where("id").is(blog.getId()));
        Update update= new Update().set("shopName", blog.getShopName()).set("isOpen", blog.getIsOpen());
        //更新查询返回结果集的第一条
        UpdateResult result =mongoTemplate.updateFirst(query,update,Shop.class);
        //更新查询返回结果集的所有
        // mongoTemplate.updateMulti(query,update,UserEntity.class);
        if(result!=null)
            return result.getMatchedCount();
        else
            return 0;
    }

    public List<Shop> findByShopName(String shopName) {
        //模糊匹配
        Pattern pattern = Pattern.compile("^.*"+ shopName +".*$", Pattern.CASE_INSENSITIVE);
        //Query query=new Query(Criteria.where("shopName").is(shopName));
        Query query=new Query(Criteria.where("shopName").regex(pattern));
        List<Shop> shop =  mongoTemplate.find(query , Shop.class);
        return shop;
    }

    public Page<Shop> findAll(Shop blog, Pageable pageable){
        Query query = new Query();
        Criteria criteria = new Criteria();

        if (StringUtils.isNotBlank(blog.getShopName())){
            //Pattern pattern = Pattern.compile("^.*"+ blog.getShopName() +".*$", Pattern.CASE_INSENSITIVE);
            criteria.and("shopName").regex(blog.getShopName());
        }

        if (blog.getIsOpen() > -1){
            criteria.and("isOpen").is(blog.getIsOpen());
        }

        if (StringUtils.isNotBlank(blog.getAddress())){
            criteria.and("address").is(blog.getAddress());
        }
        query.addCriteria(criteria);
        long count = mongoTemplate.count(query, Shop.class);
        Page<Shop> pagelist = null;
        if (count > 0){
            long skip = DataUtils.mongoSkipNum(count,pageable);
            List<Shop> shopList = mongoTemplate.find(query.limit(pageable.getPageSize()).skip(skip), Shop.class);

            pagelist = new PageImpl<Shop>(shopList, pageable, count);
        }
        return pagelist;
    }

}
